﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;

namespace SimbaRGVMonitor.Views.Components
{
    class PathItem : ArrowBase, IRectable
    {
        public PathItem()
        {
            InitializeSettings();
        }

        //====================================================================================================
        //以上是类型构造函数
        //----------------------------------------------------------------------------------------------------
        //以下是类型字段
        //====================================================================================================

        private readonly QuadraticBezierSegment quadraticBezierSegment = new QuadraticBezierSegment();
        public static readonly DependencyProperty EndPointProperty = DependencyProperty.Register("EndPoint", typeof(Point), typeof(PathItem), new FrameworkPropertyMetadata(new Point(), FrameworkPropertyMetadataOptions.AffectsMeasure));
        public static readonly DependencyProperty ControlPointProperty = DependencyProperty.Register("ControlPoint", typeof(Point), typeof(PathItem), new FrameworkPropertyMetadata(new Point(), FrameworkPropertyMetadataOptions.AffectsMeasure));

        //====================================================================================================
        //以上是类型字段
        //----------------------------------------------------------------------------------------------------
        //以下是类型属性
        //====================================================================================================

        public Point EndPoint
        {
            get { return (Point)GetValue(EndPointProperty); }
            set { this.SetValue(EndPointProperty, value); }
        }

        public Point ControlPoint
        {
            get { return (Point)GetValue(ControlPointProperty); }
            set { SetValue(ControlPointProperty, value); }
        }

        //====================================================================================================
        //以上是类型属性
        //----------------------------------------------------------------------------------------------------
        //以下是初始化设置
        //====================================================================================================

        private void InitializeSettings()
        {
            Canvas.SetZIndex(this, 2);
        }

        //====================================================================================================
        //以上是初始化设置
        //----------------------------------------------------------------------------------------------------
        //以下是实现抽象函数
        //====================================================================================================

        protected override Point GetEndArrowEndPoint()
        {
            return EndPoint;
        }

        protected override Point GetEndArrowStartPoint()
        {
            return ControlPoint;
        }

        protected override Point GetStartArrowEndPoint()
        {
            return ControlPoint;
        }

        protected override PathSegmentCollection FillFigure()
        {
            quadraticBezierSegment.Point1 = ControlPoint;
            quadraticBezierSegment.Point2 = EndPoint;

            return new PathSegmentCollection
            {
                quadraticBezierSegment
            };
        }

        //====================================================================================================
        //以上是实现抽象函数
        //----------------------------------------------------------------------------------------------------
        //以下是实现IRectable接口
        //====================================================================================================

        Rect IRectable.GetBounds()
        {
            var endPoint = EndPoint;
            var startPoint = StartPoint;
            var controlPoint = ControlPoint;

            var x =
                new[]
                {
                    startPoint.X,
                    controlPoint.X,
                    endPoint.X
                }.Min();
            var y =
                new[]
                {
                    startPoint.Y,
                    controlPoint.Y,
                    endPoint.Y
                }.Min();
            var height = 
                new[] 
                {
                    Math.Abs(endPoint.Y - startPoint.Y),
                    Math.Abs(endPoint.Y - controlPoint.Y),
                    Math.Abs(startPoint.Y - controlPoint.Y)
                }.Max();
            var width = 
                new[] 
                {
                    Math.Abs(endPoint.X - startPoint.X),
                    Math.Abs(endPoint.X - controlPoint.X),
                    Math.Abs(startPoint.X - controlPoint.X)
                }.Max();

            width = Math.Max(1, width);
            height = Math.Max(1, height);
            return new Rect(x, y, width, height);
        }
    }
}
